package library.and.xps.com.step.view.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jude.utils.JUtils;
import com.squareup.okhttp.Request;
import com.zhy.autolayout.AutoLinearLayout;

import org.greenrobot.eventbus.EventBus;

import library.and.xps.com.step.EventBusCommond;
import library.and.xps.com.step.R;
import library.and.xps.com.step.URLConstants;
import library.and.xps.com.step.m.model.ActiveDetailIntentParams;
import library.and.xps.com.step.m.model.TeamActiveList;
import library.and.xps.com.step.m.model.TeamChallengeRes;
import library.and.xps.com.step.m.netapi.OkHttpClientUtils;
import library.and.xps.com.step.net.ResponceWrapper;
import library.and.xps.com.step.util.AnimUtil;
import library.and.xps.com.step.util.CommonUtils;
import library.and.xps.com.step.util.DateUtil;
import library.and.xps.com.step.util.HttpResponsHelper;
import library.and.xps.com.step.util.RequestCallback;
import library.and.xps.com.step.util.TeamHelper;
import library.and.xps.com.step.util.UserHelper;
import library.and.xps.com.step.view.activity.CreateChallengeActivity;
import library.and.xps.com.step.view.activity.TeamActiveActivity;
import library.and.xps.com.step.view.activity.TeamActiveDetailActivity;
import library.and.xps.com.step.view.activity.TeamChallengeActivity;
import library.and.xps.com.step.view.base.BaseFragment;

public class ActCircleFragment extends BaseFragment implements View.OnClickListener {
    ImageView iv_more_active;
    ImageView iv_active_cover, iv_more_challenger;
    TextView tv_taname, tv_tatime, tv_tamember, tv_empty_hint, tv_empty_hint_active;
    AutoLinearLayout al_item;
    TeamActiveList.ListBean activeBean;

    TextView tv_challenger_name, tv_bechallenger_name, tv_challenger_exc, tv_bechallenger_exc, tv_read_time, tv_state, tv_likecount_bc, tv_likecount_c;
    ImageView iv_bechallenger_header, iv_challenger_header, iv_like_c, iv_like_bc;
    AutoLinearLayout al_state_bar, al_item_challenger, al_empty_content_challenge, al_empty_content_active;

    @Override
    protected int getRes() {
        return R.layout.fragment_activity_circle;
    }

    @Override
    protected void init(View view) {
        iv_more_active = (ImageView) view.findViewById(R.id.iv_more_active);
        iv_active_cover = (ImageView) view.findViewById(R.id.iv_active_cover);
        iv_more_challenger = (ImageView) view.findViewById(R.id.iv_more_challenger);
        iv_more_challenger.setOnClickListener(this);
        tv_likecount_bc = (TextView) view.findViewById(R.id.tv_likecount_bc);
        tv_likecount_c = (TextView) view.findViewById(R.id.tv_likecount_c);
        tv_taname = (TextView) view.findViewById(R.id.tv_taname);
        tv_tatime = (TextView) view.findViewById(R.id.tv_tatime);
        iv_like_bc = (ImageView) view.findViewById(R.id.iv_like_bc);
        iv_like_bc.setOnClickListener(this);
        iv_like_c = (ImageView) view.findViewById(R.id.iv_like_c);
        iv_like_c.setOnClickListener(this);
        tv_tamember = (TextView) view.findViewById(R.id.tv_tamember);
        al_item = (AutoLinearLayout) view.findViewById(R.id.al_item);
        al_empty_content_challenge = (AutoLinearLayout) view.findViewById(R.id.al_empty_content_challenge);
        al_empty_content_challenge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), CreateChallengeActivity.class));
            }
        });

        al_empty_content_active = (AutoLinearLayout) view.findViewById(R.id.al_empty_content_active);
        tv_empty_hint = (TextView) view.findViewById(R.id.tv_empty_hint);
        tv_empty_hint_active = (TextView) view.findViewById(R.id.tv_empty_hint_active);

        tv_challenger_name = (TextView) view.findViewById(R.id.tv_challenger_name);
        tv_bechallenger_name = (TextView) view.findViewById(R.id.tv_bechallenger_name);
        tv_challenger_exc = (TextView) view.findViewById(R.id.tv_challenger_exc);
        tv_bechallenger_exc = (TextView) view.findViewById(R.id.tv_bechallenger_exc);
        tv_read_time = (TextView) view.findViewById(R.id.tv_read_time);
        iv_bechallenger_header = (ImageView) view.findViewById(R.id.iv_bechallenger_header);
        iv_challenger_header = (ImageView) view.findViewById(R.id.iv_challenger_header);
        al_state_bar = (AutoLinearLayout) view.findViewById(R.id.al_cancel_pk);
        tv_state = (TextView) view.findViewById(R.id.tv_cancel_pk);
        al_item_challenger = (AutoLinearLayout) view.findViewById(R.id.al_item_challenger);

        iv_more_active.setOnClickListener(this);
        al_item.setOnClickListener(this);
        r2v(view, R.id.iv_menubt).setOnClickListener(this);

    }

    @Override
    public void onResume() {
        super.onResume();
        if (TeamHelper.getIns().hasTeam()) {
            requestTeamActives();
            requestTeamChallengers();
        }
    }

    public void hide() {
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.hide(this);
        fragmentTransaction.commitAllowingStateLoss();
    }

    @Override
    protected void setData(View view) {
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_more_active:
                startActivity(new Intent(getActivity(), TeamActiveActivity.class));
                break;
            case R.id.al_item:
                startActiveDetail();
                break;
            case R.id.iv_more_challenger:
                startActivity(new Intent(getActivity(), TeamChallengeActivity.class));
                break;
            case R.id.iv_menubt:
                EventBus.getDefault().post(EventBusCommond.OPEN_DRAWERMENU);
                break;
            case R.id.iv_like_bc:
                AnimUtil.scaleAnim(iv_like_bc, 400);
                break;
            case R.id.iv_like_c:
                AnimUtil.scaleAnim(iv_like_c, 400);
                break;
        }
    }

    /**
     *  跳转到团队详情
     * */
    void startActiveDetail() {
        if (activeBean == null) return;
        Intent i = new Intent(getActivity(), TeamActiveDetailActivity.class);
        ActiveDetailIntentParams activeDetailIntentParams = new ActiveDetailIntentParams();
        activeDetailIntentParams.setDescribe(activeBean.getDescribe());
        activeDetailIntentParams.setEndtime(activeBean.getEndtime());
        activeDetailIntentParams.setStarttime(activeBean.getStarttime());
        activeDetailIntentParams.setActivityid(activeBean.getActivityid());
        activeDetailIntentParams.setMark(activeBean.getMark());
        activeDetailIntentParams.setPeople(activeBean.getPeople());
        activeDetailIntentParams.setState(activeBean.getState());
        activeDetailIntentParams.setTeamid(activeBean.getTeamid());
        activeDetailIntentParams.setTaname(activeBean.getTaname());
        activeDetailIntentParams.setExercise(activeBean.getExercise());
        i.putExtra("active", activeDetailIntentParams);
        startActivity(i);
    }

    /**
     * 使用团队挑战的第一个填充布局
     * */
    void inflateFirstItemWithChallenge(TeamChallengeRes team) {
        if (team.getList().size() > 0) {
            al_empty_content_challenge.setVisibility(View.GONE);
            final TeamChallengeRes.ListBean data = team.getList().get(0);
            initChallengerState(data.getState());
            CommonUtils.setupCircleImgWithToken(getActivity(), iv_bechallenger_header, data.getBico());
            CommonUtils.setupCircleImgWithToken(getActivity(), iv_challenger_header, data.getCico());
            tv_read_time.setText(data.getReadingtimes());
            tv_bechallenger_exc.setText(data.getBsteps() + "");
            tv_challenger_exc.setText(data.getSteps() + "");
            tv_challenger_name.setText(data.getCname() + "");
            tv_bechallenger_name.setText(data.getBname() + "");
        } else {
            al_empty_content_challenge.setVisibility(View.VISIBLE);
        }
    }

    /**
     *  使用团队活动的第一个填充布局
     * */
    void inflateFirstItemWithActive() {
        CommonUtils.setUpimg(getActivity(), iv_active_cover, activeBean.getMark(), false);
        tv_taname.setText("活动:" + activeBean.getTaname());
        tv_tatime.setText("开始时间:" + DateUtil.retrofitDateString(activeBean.getStarttime()));
        tv_tamember.setText("参与人数:" + activeBean.getPeople());
    }

    /**
     *  获取团队活动
     * */
    void requestTeamActives() {
        String data = URLConstants.TEAMACTIVE.TEAM_ACTIVITIES + "?" + "userid=" + getUser().getUserid() + "&teamid=" + getUser().getTeams().get(0).getTId() + "&page=" + 1;
        OkHttpClientUtils.getDataAsync(getActivity(), data, new RequestCallback() {
            @Override
            public void onFailure(Request request, Exception e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(ResponceWrapper response) {
                if (HttpResponsHelper.SuccessResponce(response)) {
                    al_empty_content_active.setVisibility(View.GONE);
                    TeamActiveList teamActiveList = new Gson().fromJson(response.getBody(), TeamActiveList.class);
                    if (teamActiveList.getList() != null && teamActiveList.getList().size() > 0) {
                        activeBean = teamActiveList.getList().get(0);
                        inflateFirstItemWithActive();
                    }
                } else {
                    al_empty_content_active.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onStart() {

            }
        }, "");
    }

    /**
     * 获取所有团队挑战
     */
    public void requestTeamChallengers() {
        String request = URLConstants.PERSONACTIVE.GETALLUSERPK + "?" + "teamid=" + TeamHelper.getIns().getCurT().getTeamid() + "&userid=" + UserHelper.getInstance().getUser().getUserid() + "&" + "page=" + 1;
        OkHttpClientUtils.getDataAsync(getActivity(), request, new RequestCallback() {

            @Override
            public void onFailure(Request request, Exception e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(ResponceWrapper response) {
                if (HttpResponsHelper.SuccessResponce(response)) {
                    TeamChallengeRes team = new Gson().fromJson(response.getBody(), TeamChallengeRes.class);
                    inflateFirstItemWithChallenge(team);
                } else {
                    al_empty_content_challenge.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onStart() {

            }
        }, "");
    }

    /**
     * 设置挑战当前当前状态
     */
    private void initChallengerState(int state) {
        switch (state) {
            case -1://已取消
                al_state_bar.setBackgroundColor(Color.parseColor("#f4f4f4"));
                tv_state.setText("已取消");
                tv_state.setTextColor(Color.parseColor("#7e7e7e"));
                break;
            case 0://等待响应
                al_state_bar.setBackgroundColor(Color.parseColor("#FF489DFB"));
                tv_state.setText("等待响应");
                tv_state.setTextColor(Color.WHITE);
                break;
            case 1://即将开始
                al_state_bar.setBackgroundColor(Color.parseColor("#FF489DFB"));
                tv_state.setText("即将开始");
                tv_state.setTextColor(Color.WHITE);
                break;
            case 2://进行中
                al_state_bar.setBackgroundColor(Color.parseColor("#f67070"));
                tv_state.setText("进行中");
                tv_state.setTextColor(Color.WHITE);
                break;
            case 3://拒绝
                al_state_bar.setBackgroundColor(Color.parseColor("#FFD700"));
                tv_state.setText("拒绝");
                tv_state.setTextColor(Color.WHITE);
                break;
            case 4://结束
                al_state_bar.setBackgroundColor(Color.parseColor("#f4f4f4"));
                tv_state.setText("结束");
                tv_state.setTextColor(Color.parseColor("#7e7e7e"));
                break;
        }
    }


}
